/**
 * Created by Administrator on 2018/3/30.
 */
jQuery(document).ready(function () {
    sysPram.getSysPram();
    var btnElement = '#isSubmit .dis';
    btnJurisdiction(btnElement);//查询当前用户的按钮权限
});
$(function(){
    sysPram.bootstrapValidator();
})

var sysPrams = []

var sysPram = {};

(function (res) {
    //表单验证
    res.bootstrapValidator=function(){
        $("#sysParamForm").bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                validating: 'glyphicon glyphicon-refresh'
            },
            fields:{
                djzl:{
                    validators: {
                        notEmpty: {
                            message: '请输入验证码有效期'
                        }
                    }
                }
            }
        });
    };
    //防止表单重复验证
    res.resetValidator=function(){
        $("form.required-validate").each(function() {
            var $form = $(this);
            $form.bootstrapValidator().on("success.form.bv",function(e){
                e.preventDefault();
            });
        });
    };
    res.getSysPram = function () {
        $.ajax({
            url:"../sysPramController/getSysPramList",
            //data:{"menuName":"系统参数设置","methodName":"保存系统参数"},
            type:"post",
            dataType:"json",
            success:function(res){
                sysPrams = res.data;
                var html = ''
                for (i=0;i<res.data.length;i++) {
                    if (res.data[i].valflag==0) {
                        if (res.data[i].flag==0) {
                            html += '<div id="sysPram_'+i+'" class="form-group" style="margin: 10px 0px 10px 5px;">'
                                +'<label id="name" class="col-sm-3 control-label">'+res.data[i].name+':</label>'
                                +'<div class="col-sm-9">'
                                +'<input type="text" id="value" value="'+res.data[i].value+'" name="yxq'+i+'" style="width: 300px" class="form-control" placeholder="请输入代码">'
                                +'</div>'
                                +'</div>'
                        }else {
                            html += '<div id="sysPram_'+i+'" class="form-group" style="margin: 10px 0px 10px 5px;">'
                                +'<label id="name" class="col-sm-3 control-label">'+res.data[i].name+':</label>'
                                +'<div class="col-sm-9">'
                                +'<input type="text" id="value" readonly value="'+res.data[i].value+'" name="dsyj'+i+'" style="width: 300px" class="form-control" placeholder="请输入代码">'
                                +'</div>'
                                +'</div>'
                        }
                    }
                    if (res.data[i].valflag==1){
                        if (res.data[i].flag==0) {
                            html += '<div id="sysPram_'+i+'" class="form-group" style="margin: 10px 0px 10px 5px;">'
                                +'<label id="name" class="col-sm-4 control-label">'+res.data[i].name+':</label>'
                                +'<div class="col-sm-8">'
                                +'<input type="number" id="value" value="'+res.data[i].value+'" name="djzl'+i+'" style="width: 300px" class="form-control" placeholder="请输入代码">'
                                +'</div>'
                                +'</div>'
                        }else {
                            html += '<div id="sysPram_'+i+'" class="form-group" style="margin: 10px 0px 10px 5px;">'
                                +'<label id="name" class="col-sm-3 control-label">'+res.data[i].name+':</label>'
                                +'<div class="col-sm-9">'
                                +'<input type="number" id="value" readonly value="'+res.data[i].value+'" name="zjzl'+i+'" style="width: 300px" class="form-control" placeholder="请输入代码">'
                                +'</div>'
                                +'</div>'
                        }
                    }
                    if (res.data[i].valflag==2) {
                        if (res.data[i].flag==0) {
                            html += '<div id="sysPram_'+i+'" class="form-group" style="margin: 10px 0px 10px 5px;">'
                                +'<label id="name" class="col-sm-3 control-label">'+res.data[i].name+':</label>'
                                +'<div class="col-sm-9">'
                                +'<select id="isparent" class="form-control" name="isparent">'
                                +'<option value="0">否</option>'
                                +'<option value="1">是</option>'
                                +'</select>'
                                +'</div>'
                                +'</div>'
                        }else {
                            html += '<div id="sysPram_'+i+'" class="form-group" style="margin: 10px 0px 10px 5px;">'
                                +'<label id="name" class="col-sm-3 control-label">'+res.data[i].name+':</label>'
                                +'<div class="col-sm-9">'
                                +'<select disabled id="isparent" class="form-control" name="isparent">'
                                +'<option value="0">否</option>'
                                +'<option value="1">是</option>'
                                +'</select>'
                                +'</div>'
                                +'</div>'
                        }
                    }
                }
                $("#sysParamDiv").html(html);
            }
        })
    }
    
    res.submitSysPram = function () {
        var $form = $('#sysParamForm');
        var data = $form.data('bootstrapValidator');
        if (data) {
            // 修复记忆的组件不验证
            data.validate();
            if (!data.isValid()) {
                return false;
            }
        }
        res.resetValidator();

        var data = [];
        var obj = {};
        for (i = 0; i < sysPrams.length; i++) {
            obj = sysPrams[i];
            obj.value = $("#sysPram_" + i).find("#value").val();
            obj.menuName = "系统参数设置";
            obj.methodName = "保存系统参数";
            data.push(obj);
        }
        var pram = JSON.stringify(data);
        $.ajax({
            url: "../sysPramController/upDateSysPram",
            data:pram ,
            type: "post",
            contentType:"application/json",
            dataType: "json",
            success: function (res) {
                if (res.code==0) {
                    layer.msg("配置成功");
                }
            }
        });
    }

})(sysPram)